package practice9;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class P8654 {


    static int[] s=new int[1010*1010];

    //查找
    public static int find(int x){
        while (s[x]>=0){
            x=s[x];
        }
        return x;
    }

    //合并
    public static void union(int a,int b){
        int x = find(a);
        int y = find(b);
        if(x==y) return;
        if(s[x]>s[y]){
            s[y]+=s[x];     //这两步顺序不能颠倒
            s[x]=y;
        }else{
            s[x]+=s[y];
            s[y]=x;
        }
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();
        int k=sc.nextInt();
        Arrays.fill(s,-1);
        for(int i=0;i<k;i++){
            int a=sc.nextInt();
            int b=sc.nextInt();
            union(a,b);
        }
        Set<Integer> set=new HashSet<>();
        for(int i=1;i<=m*n;i++){
            set.add(find(i));
        }
        System.out.println(set.size());
    }
}
